Python: Fix AG-UI tool history replay sanitization - #6581
Merged
moonbox3 merged 2 commits intoJun 19, 2026
Conversation
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the Python AG-UI message replay path so outbound provider messages remain contract-valid when tool-call sequences are interrupted, incomplete, or interleaved, and adds regression coverage for HITL approval replay so approved tool outputs are persisted for subsequent replays.
Changes:
- Extend
_sanitize_tool_historyto deterministically close pending assistant tool calls by injecting synthetic tool results before later assistant messages, non-standard roles, or end-of-history. - Preserve tool-call ordering when injecting synthetic results by tracking pending tool call ids in-order.
- Add tests covering consecutive assistant tool-call messages, histories ending with unresolved tool calls, and HITL approval snapshots replaying as tool results (not raw approval payloads).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py | Refactors and extends tool-history sanitization to inject synthetic tool results at more replay boundary points, using ordered pending call tracking. |
| python/packages/ag-ui/tests/ag_ui/test_message_adapters.py | Adds regression tests for consecutive assistant tool-call messages, end-of-history pending tool calls, and updated behavior for non-standard roles. |
| python/packages/ag-ui/tests/ag_ui/test_approval_result_event.py | Adds coverage ensuring approved HITL tool executions are persisted into MESSAGES_SNAPSHOT for replay, and that replay normalizes to provider-valid function_result content. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
eavanvalkenburg
approved these changes
Jun 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation & Context
AG-UI replay can reconstruct assistant/tool histories that violate provider message contracts, causing OpenAI/Azure OpenAI requests to fail when assistant
tool_callsare not followed by matching tool result messages.This change keeps replayed AG-UI history provider-valid for interrupted or incomplete tool-call sequences, and adds coverage for approved HITL tool results being persisted into
MESSAGES_SNAPSHOTso the next replay does not re-send raw approval payloads.Description & Review Guide
What are the major changes?
MESSAGES_SNAPSHOT.What is the impact of these changes?
What do you want reviewers to focus on?
Related Issue
Fixes #5855
Fixes #6513
No other open PR was found for these issues.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.